import Dialog from "@vant/weapp/dialog/dialog";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    moreInfo: {},
    isBind: "1",
    nameObj: {
      sec_admin: "二级管理员",
      cbrc: "银监用户",
      fir_admin: "一级管理员",
      manager: "客户经理",
      chat_user: "chat"
    },
    type: "",
  },

  goBind(e) {
    if (e.currentTarget.dataset.type === "jcbd") {
      Dialog.confirm({
        title: "",
        message: "是否确认与当前所在机构解除绑定",
      })
        .then(() => {
          const params = {
            userId: this.data.userInfo.userId,
          };
          getApp()
            .globalData.request("/user/unbind", params, "POST")
            .then((res) => {
              console.log(res, "解除绑定");
              // this.getMoreInfo(this.data.userInfo.userId)
              wx.navigateBack({
                delta: 1,
              });
            });
        })
        .catch(() => {
          // on cancel
        });
    } else {
      wx.navigateTo({
        url: "/pages/accountBind/accountBind?type=1",
      });
    }
  },

  onclick() {},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const userInfo = wx.getStorageSync("userInfo");
    this.setData({
      type: options.type,
    });
    wx.setNavigationBarTitle({
      title:
        options.type === "1"
          ? "个人详情"
          : options.type === "2"
          ? "机构信息"
          : "账号信息",
    });
    // console.log(userInfo, 'userInfo')
    userInfo.avatarUrl = userInfo.avatarUrl
      ? userInfo.avatarUrl
      : "https://zx-1311146360.cos.ap-shanghai.myqcloud.com/personalCenter/2.png";
    this.setData({
      userInfo: userInfo,
    });
    this.getMoreInfo(userInfo.userId);
  },

  getMoreInfo(id) {
    let prams = {
      userId: id,
    };
    console.log(prams);
    getApp()
      .globalData.request("/corpVisit/query_user_info", prams, "POST")
      .then((res) => {
        console.log(res, "moreInfo");
        // res.data.roleName = res.data.roles.length > 0 ? res.data.roles.map(val => val.roleName).join(',') : ''
        if (res.data) {
          res.data.roles.forEach((pre) => {
            pre.name = this.data.nameObj[pre.roleName];
          });
          res.data.roleName =
            res.data.roles.length > 0
              ? res.data.roles.map((val) => val.name).join(",")
              : "";
          this.setData({
            moreInfo: res.data,
          });
        } else {
          this.setData({
            moreInfo: {},
          });
        }
      });
  },

  async loginOut() {
    const res = await getApp().globalData.request("/login/logout", {}, "POST");
    if (res) {
      wx.removeStorageSync("token");
      wx.removeStorageSync("userInfo");
      wx.switchTab({
        url: "/pages/shouye/shouye",
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {},
});
